<?php
/**
 * Created by PhpStorm.
 * User: ZL
 * Date: 2016/5/21
 * Time: 10.37
 */
namespace ZL;
use \Think\Controller;

class Community extends Controller
{
    public function restAll(){
        $where1['status']=1;
        $communityData = D('Community')->where($where1)->select();
        $data = array();
        foreach ($communityData as $v) {
            if($v['children_count']==0)
                continue;

            $data[$v['id']] = $this->getCommunity($v['id']);
        }
//        myDump($data);die;
        return $data;
    }

    public function getAllCommunity(){
        if(S('communityData'))
            return S('communityData');
        $where1['status']=1;
        $communityData = D('Community')->where($where1)->select();
        $data = array();
        foreach ($communityData as $v) {
            if($v['children_count']==0)
                continue;
            if(S('community'.$v['id']))
                $data[$v['id']] = S('community'.$v['id']);
            else
                $data[$v['id']] = $this->getCommunity($v['id']);
        }
//        S('communityData', $data);
//        myDump($data);die;
        return $data;
    }
    //可选小区获取
    public function selectCommunity($communityStr){
        $where1['status']=1;
        $where1['id']=array('in', $communityStr);
        $communityData = D('Community')->where($where1)->select();
        $data = array();
        foreach ($communityData as $v) {
            if($v['children_count']==0)
                continue;
            if(S('community'.$v['id']))
                $data[$v['id']] = S('community'.$v['id']);
            else
                $data[$v['id']] = $this->getCommunity($v['id']);
        }
//        myDump($data);die;
        return $data;
    }
    //取得单个小区的数据
    public function getCommunity($communityId){
        $where['status']=1;
        $where['community_id'] = array('eq',$communityId);
        $where1['status']=1;
        $where1['id'] = array('eq',$communityId);
        $communityData = D('Community')->where($where1)->select();
        $zoneData = D('Zone')->where($where)->select();
        $buildingData = D('Building')->where($where)->select();
        $unitData = D('Unit')->where($where)->select();
        $roomData = D('Room')->where($where)->select();

        foreach ($communityData as $k=>$v) {
            if($v['children_count']==0)
                continue;
            $result[$v['id']]['community_id'] = $v['id'];
            $result[$v['id']]['community_name'] = $v['name'];

            foreach ($zoneData as $k1=>$v1) {
                if($v1['community_id']==$v['id']) {
                    $result[$v['id']]['data'][$v1['id']]['zone_id'] = $v1['id'];
                    $result[$v['id']]['data'][$v1['id']]['zone_name'] = $v1['name'];

                    foreach ($buildingData as $k2 => $v2) {
                        if($v2['zone_id']==$v1['id']){
                            $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['building_id'] = $v2['id'];
                            $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['building_name'] = $v2['name'];

                            foreach ($unitData as $k3 => $v3){
                                if($v3['building_id']==$v2['id']){
                                    $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['unit_id'] = $v3['id'];
                                    $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['unit_name'] = $v3['name'];

                                    foreach ($roomData as $k4 => $v4){
                                        if($v4['unit_id']==$v3['id']){
                                            $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['data'][$v4['id']]['room_id'] = $v4['id'];
                                            $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['data'][$v4['id']]['room_name'] = $v4['name'];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            $this->createCache($v['id'], $result[$v['id']]);
        }
        return $result[$communityId];
    }

    public function createCache($id,$data){
        S('community' . $id, $data);

        $allData = S('communityData');
        $allData[$id] = $data;
        S('communityData',$allData);
    }

}